/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package x10.interfaces.cm15a.receive;

import x10.utilities.ChunkOfBytes;
import x10.enums.Interfaces;
import x10.event.X10Event;

/**
 *
 * @author Denny
 */
public class CM15AReceiveFactory {
    public static final byte interfaceMessage = (byte)0x55;
    public static final byte plMessage = (byte)0x5A;
    public static final byte macroMessage = (byte)0x5B;
    public static final byte rfMessage = (byte)0x5D;
    public static final Interfaces interfaceType = Interfaces.CM15A;

    X10DeviceReceiver receiver;
    LastAddressedTracker lat = new LastAddressedTracker();

    public CM15AReceiveFactory() {
    }

    public X10Event dispatch(ChunkOfBytes bytes){
        X10Event device = null;
        byte messageType;

        //delete ...
        //device = stat.process(bytes, interfaceType.toString());

        messageType = bytes.getBytes()[0];  //message type is in the first byte
        if(bytes.isInterfaceRequest() || (messageType == interfaceMessage)){
            receiver = new IFResponseFactory(lat, interfaceType);
            device = receiver.process(bytes);
        } else if(messageType == rfMessage) {
            receiver = new RFMessageFactory(lat, interfaceType);
            device = receiver.process(bytes);
        } else if(messageType == plMessage){
            receiver = new PLMessageFactory(lat, interfaceType);
            device = receiver.process(bytes);
        } else {
            System.out.println("Exception: unrecognized CM15A byte array: ");
            System.out.println(bytes.toString());
        }

        return device;
    }
}
